package com.hyr.springcloud.ticket.controller;

import com.hyr.springcloud.dto.OrderDTO;
import com.hyr.springcloud.dto.TicketDTO;
import com.hyr.springcloud.ticket.dao.TicketRespository;
import com.hyr.springcloud.ticket.demain.Ticket;
import com.hyr.springcloud.ticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

@RestController
@RequestMapping(value = "/api/ticket")
public class TicketController {

    @Autowired
    private TicketRespository ticketRespository;

    @Autowired
    private TicketService ticketService;

    @Autowired
    private JmsTemplate jmsTemplate;

    @RequestMapping("/{id}")
    public TicketDTO getTicket(@PathVariable(value = "id") Integer id){
        return ticketService.getTicket(id);
    }

//    @PostConstruct
//    public void init(){
//        Ticket ticket = new Ticket();
//        ticket.setName("Num.1");
//        ticket.setTicketNum(1000);
//        ticketRespository.save(ticket);
//    }

    @PostMapping("/lock")
    public Ticket lock( OrderDTO dto){
        /*
        * 可使用分布式锁，使业务方法同时只能一个线程访问
        * */
        return ticketService.ticketLock(dto);
    }

    @PostMapping("/lock2")
    public int lock2( OrderDTO dto){
        return ticketService.ticketLock2(dto);
    }
}
